﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cms.Application;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain;
using Cms.Domain.Authentication;
using Cms.Web.Framework.Infrastructure;
using iLib.Web.UI;
using Page = Cms.Domain.Page;

namespace Cms.Web.UI.Admin
{
    public partial class PublicAdminMenu : UserControl, IPublicAdminMenuView
    {
        private PublicAdminMenuPresenter _presenter;
        private RequestDetails _requestDetails;

        #region IPublicAdminMenuView Members

        public int AdministratorID
        {
            get
            {
                AuthenticatedUser user = Conversion.ToAuthenticatedUser(Page.User.Identity.Name);
                return user.AdministratorID;
            }
        }


        public string ContextTranslationFile
        {
            get { return "Admin/Translations/PublicAdminMenu.xml"; }
        }


        public string Localization
        {
            get
            {
                HttpCookie cookie = Request.Cookies["FrediumAdminLocalization"];
                if (cookie != null && cookie.Value != null)
                    return cookie.Value;
                else
                    return "en-US";
            }
        }


        public void SetLatestVisitedAdminUrls(IList<VisitedAdminUrl> urls)
        {
            rpLatestVisitedAdminUrls.DataSource = urls;
            rpLatestVisitedAdminUrls.DataBind();
        }


        public Page CurrentPage
        {
            get { return _requestDetails.Page; }
        }


        public void SetEditLinks(Page currentPage)
        {
            if (currentPage is ApplicationPage)
            {
                hlEditPageContent.NavigateUrl = "~/Admin/EditApplicationPage.aspx?id=" + currentPage.ID + "&tab=content";
                hlEditPageLocalizedProperties.NavigateUrl = "~/Admin/EditApplicationPage.aspx?id=" + currentPage.ID +
                                                            "&tab=localizedproperties";
                hlEditPageBasicProperties.NavigateUrl = "~/Admin/EditApplicationPage.aspx?id=" + currentPage.ID +
                                                        "&tab=basicproperties";
                hlEditPageDesign.NavigateUrl = "~/Admin/EditApplicationPage.aspx?id=" + currentPage.ID + "&tab=design";
                hlEditPageCode.NavigateUrl = "~/Admin/EditApplicationPage.aspx?id=" + currentPage.ID + "&tab=code";
            }
            else
            {
                hlEditPageContent.NavigateUrl = "~/Admin/EditPage.aspx?id=" + currentPage.ID + "&tab=content";
                hlEditPageLocalizedProperties.NavigateUrl = "~/Admin/EditPage.aspx?id=" + currentPage.ID +
                                                            "&tab=localizedproperties";
                hlEditPageBasicProperties.NavigateUrl = "~/Admin/EditPage.aspx?id=" + currentPage.ID +
                                                        "&tab=basicproperties";
                hlEditPageDesign.Visible = false;
                hlEditPageCode.Visible = false;
            }
            hlEditDetailTemplateContent.NavigateUrl = "~/Admin/EditDetailTemplate.aspx?id=" +
                                                      currentPage.DetailTemplate.ID + "&tab=content";
            hlEditDetailTemplateProperties.NavigateUrl = "~/Admin/EditDetailTemplate.aspx?id=" +
                                                         currentPage.DetailTemplate.ID + "&tab=properties";
            hlEditMasterTemplateContent.NavigateUrl = "~/Admin/EditMasterTemplate.aspx?id=" +
                                                      currentPage.MasterTemplate.ID + "&tab=content";
            hlEditMasterTemplateCss.NavigateUrl = "~/Admin/EditMasterTemplate.aspx?id=" + currentPage.MasterTemplate.ID +
                                                  "&tab=css";
            hlEditMasterTemplateProperties.NavigateUrl = "~/Admin/EditMasterTemplate.aspx?id=" +
                                                         currentPage.MasterTemplate.ID + "&tab=properties";
        }

        public void HideEditLink()
        {
            hlEdit.Visible = false;
        }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _requestDetails = (RequestDetails) HttpContext.Current.Items["RequestDetails"];
            _presenter = new PublicAdminMenuPresenter(this);
            Page.Header.Controls.Add(new iLibRegistrator());
            hlLatestVisitedAdminUrls.Text = Translator.GetText(ContextTranslationFile, "hlLatestVisitedAdminUrls", Localization);
            hlEdit.Text = Translator.GetText(ContextTranslationFile, "hlEdit", Localization);
            _presenter.Initialize();
        }


        protected void rpLatestVisitedAdminUrls_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;
            var visit = (VisitedAdminUrl) e.Item.DataItem;
            var litUrl = (Literal) e.Item.FindControl("litUrl");
            litUrl.Text = "<a href=\"" + visit.Url + "\"><img src=\"" + visit.ImageUrl + "\" /><span>" + visit.Header +
                          "</span></a>";
        }
    }
}